package com.laoxu.demo.bootstrapcurd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.laoxu.demo.bootstrapcurd.model.User;
import com.laoxu.demo.bootstrapcurd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author laoxu
 * @Date 2020/12/17 17:06
 **/
@RequestMapping
@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @Autowired
    UserService userService;

    @PostMapping("/doLogin")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpServletRequest request, HttpServletResponse response){
        QueryWrapper<User> param = new QueryWrapper<>();
        param.eq("username", username);
        User user = userService.getOne(param);
        // 未找到用户
        if(user == null){
            model.addAttribute("error","用户不存在");
            return "login";
        }

        if(!password.equals(user.getPassword())){
            model.addAttribute("error","密码错误");
            return "login";
        }

        model.addAttribute("username",username);
        request.getSession().setAttribute("user",user);

        return "redirect:/index";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "login";
    }
}
